<template>
  <div>
    <el-table
      :data="permissionList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="code" label="权限编码" />
      <el-table-column label="操作" width="300">
        <template #default="{ row }">
          <el-button
            type="primary"
            :disabled="row.level == 4"
            size="small"
            @click="addClick(row)"
          >
            {{ row.level == 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button
            type="primary"
            :disabled="row.level == 1"
            size="small"
            @click="editClick(row)"
          >
            编辑
          </el-button>
          <el-popconfirm
            :title="`确定删除${row.name}吗？`"
            width="210"
            @confirm="deletePermission(row.id)"
            confirm-button-text="确定"
            cancel-button-text="取消"
          >
            <template #reference>
              <el-button
                type="danger"
                size="small"
                icon="delete"
                title="删除"
                :disabled="row.level == 1"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      v-model="menuDialogVisible"
      :title="permissionData.pid ? '添加菜单' : '编辑菜单'"
      width="500"
    >
      <el-form :model="permissionData" label-position="right">
        <el-form-item label="名称" label-width="100px">
          <el-input v-model="permissionData.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="权限编码" label-width="100px">
          <el-input v-model="permissionData.code" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="menuDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveOrUpdate()">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import {
  reqAddPermission,
  reqDeletePermission,
  reqPermissionList,
  reqUpdatePermission,
} from '@/api/acl/permission'
import type { PermissionData } from '@/api/acl/permission/type.ts'
import { ElMessage } from 'element-plus'

const permissionList = ref<PermissionData[]>([])

const menuDialogVisible = ref(false)
const permissionData = ref<any>({
  name: '',
  code: '',
  pid: '',
  id: '',
})

onMounted(() => {
  getPermissionList()
})

const getPermissionList = async () => {
  let res = await reqPermissionList()
  console.log('permissionList=', res)
  if (res.code === 200) {
    permissionList.value = res.data
  }
}
//添加
const addClick = (data: any) => {
  permissionData.value = {
    name: '',
    code: '',
    pid: data.id,
    level: data.level + 1,
    type: data.level > 3 ? 2 : 1,
  }
  menuDialogVisible.value = true
}
//编辑
const editClick = (data: any) => {
  permissionData.value = data
  menuDialogVisible.value = true
}
const saveOrUpdate = async () => {
  if (permissionData.value.id) {
    //编辑
    let res = await reqUpdatePermission(permissionData.value)
    if (res.code === 200) {
      ElMessage.success('编辑成功')
      getPermissionList()
      menuDialogVisible.value = false
    } else {
      ElMessage.error('编辑失败')
    }
  } else {
    //添加
    let res = await reqAddPermission(permissionData.value)
    if (res.code === 200) {
      ElMessage.success('添加成功')
      getPermissionList()
      menuDialogVisible.value = false
    } else {
      ElMessage.error('添加失败')
    }
  }
}
const deletePermission = async (id: number) => {
  let res = await reqDeletePermission(id)
  if (res.code === 200) {
    getPermissionList()
    ElMessage.success('删除成功')
  } else {
    ElMessage.error('删除失败')
  }
}
</script>
<style scoped lang="scss"></style>
